package com.example.demo.common;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class ApiMesResult<T> {
    private String msg;
    private Boolean success;
    private T Data;


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public T getData() {
        return Data;
    }

    public void setData(T data) {
        this.Data = data;
    }


    public ApiMesResult() {
    }

    public ApiMesResult(T data) {
        this.Data = data;
    }

    public static NewResult success() {
        NewResult newResult = new NewResult<>();
//        result.setCode("0");
        newResult.setSuccess(true);
        newResult.setErrorCode(0);
        newResult.setMsg("请求成功!");
        return newResult;
    }

    public static <T> String success(T data) {
        ApiMesResult<T> apiMesResult = new ApiMesResult<>(data);
        apiMesResult.setSuccess(true);
        apiMesResult.setMsg("请求成功");
        JSONObject json = JSONUtil.parseObj(apiMesResult, false);
//        String jsonstring = JSON.toJSONString(result);
//        return "ppp("+json.toStringPretty()+")";
        return json.toStringPretty();
    }

    public static NewResult error(Integer code, String msg) {
        NewResult newResult = new NewResult();
        newResult.setSuccess(false);
        newResult.setErrorCode(code);
        newResult.setMsg(msg);
        return newResult;
    }
}
